//
//  SelectDatePickerView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/23.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class SelectDatePickerView: SelectPickerView, UITableViewDataSource, UITableViewDelegate {
    var dataArr:NSArray = KDatedata as NSArray
    var cellArr:Array<BaseTableViewCell> = []
    lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.clear
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.isScrollEnabled = false
        tableView.estimatedRowHeight = 0
        tableView.estimatedRowHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        return tableView
    }()
    
    override init(style:selectStyle, title:String, selectStr:AnyObject?, dataArr:Array<Any>, dataSource:SlecterProcotol) {
        super.init(style:style, title: title, selectStr:selectStr, dataArr: dataArr, dataSource:dataSource)
        if selectStr != nil {
            self.selectDateArr = (selectStr as? Array)!
        }
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() -> Void {
        self.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.top.right.left.bottom.equalToSuperview()
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let identy = "identyCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: identy) as? BaseTableViewCell
        if cell == nil {
            cell = BaseTableViewCell(style: .default, reuseIdentifier: identy)
            cell?.contentView.addSubview((cell?.selectImage)!)
            cell?.selectImage.image = UIImage(named: "cellSelected")
            cell?.selectImage.snp.makeConstraints({ (make) in
                make.right.equalToSuperview().offset(-15)
                make.centerY.equalToSuperview()
            })
            cell?.selectionStyle = .none
            cell?.tag = KCellTag + indexPath.row
            cellArr.append(cell!)
        }
        cell?.selectImage.isHidden = true
        if self.selectDateArr.contains(indexPath.row) {
            cell?.selectImage.isHidden = false
            cell?.selectStateBool = true
        }
        if self.selectDateArr.contains(7) && indexPath.row == 0{
            cell?.selectImage.isHidden = false
            cell?.selectStateBool = true
        }
        cell?.nameLabel.text = dataArr.object(at: indexPath.row) as? String
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.1
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = self.viewWithTag(KCellTag + indexPath.row) as! BaseTableViewCell
        cell.selectStateBool = !cell.selectStateBool
        cell.selectImage.isHidden = !cell.selectStateBool
        if cell.selectStateBool {
            self.selectDateArr.append(indexPath.row+1)
        }
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
